//
//  LSYnewCommentsView.m
//  Cartoon
//
//  Created by dllo on 15/7/16.
//  Copyright (c) 2015年 dllo. All rights reserved.
//
#import "BaseHeader.h"
#import "LSYnewCommentsView.h"
#import "commentTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "AFNetworkHandler.h"

@interface LSYnewCommentsView ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, retain) UITableView *NewtableView;
@property (nonatomic, retain) NSMutableArray *CommentsNewDataArr;
@property (nonatomic, assign) NSInteger commentsCount;
@property (nonatomic, retain) NSString *str;
@property (nonatomic, retain) NSString *idStr;

@end
@implementation LSYnewCommentsView
- (void)dealloc
{
    [_idStr release];
    [_str release];
    [_NewtableView release];
    [_CommentsNewDataArr release];
    [super dealloc];
    
}

- (instancetype)initWithFrame:(CGRect)frame URL:(NSString *)URL
{
    self = [super initWithFrame:frame];
    if (self) {
        self.commentsCount = 40;
        self.idStr = URL;
        [self getData:URL];
    }
    return self;
}

- (void)createTableView:(NSString *)newURL
{
    
    
    self.NewtableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    self.NewtableView.delegate = self;
    self.NewtableView.dataSource = self;
    self.NewtableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 15);
    self.NewtableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self addSubview:self.NewtableView];
    [_NewtableView release];
    self.NewtableView.bounces = NO;
}

#pragma mark - tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.CommentsNewDataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    commentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"l"];
    if (!cell) {
        cell = [[[commentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"l"] autorelease];
    }
    //取消选中效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.CommentsNewDataArr[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSYFastLookHotCommentsModel *model = self.CommentsNewDataArr[indexPath.row];
    CGFloat h = [commentTableViewCell heightWithText:model.content];
    CGFloat f = h + 80 * SCREEN_HEIGHT / 667;
    return f;
}

+ (instancetype)newcommentsWithinitFrame:(CGRect)frame URL:(NSString *)URL
{
    id result = [[self alloc] initWithFrame:frame URL:URL];
    return result;
}


- (void)getData:(NSString *)newURL
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSString *strTemp = [@"http://api.kuaikanmanhua.com/v1/comics/" stringByAppendingString:newURL];
        NSString *strtemp = [strTemp stringByAppendingString:@"/comments?order=&offset=0&limit="];
        NSString *commentCount = [NSString stringWithFormat:@"%ld", self.commentsCount];
        self.str = [strtemp stringByAppendingString:commentCount];
       
        [AFNetworkHandler GETwithAFNByURL:self.str completion:^(id result) {
            NSDictionary *tempDic = [result objectForKey:@"data"];
            NSArray *tempArr = [tempDic objectForKey:@"comments"];
            self.CommentsNewDataArr = [LSYFastLookHotCommentsModel arrayWithModelByArray:tempArr];
            NSLog(@"%ld", self.CommentsNewDataArr.count);
            [self createTableView:newURL];
        }];
    });
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    NSLog(@"%.2f", self.NewtableView.contentOffset.y);
//    NSLog(@"%.2f", self.NewtableView.contentSize.height);
//    if (self.NewtableView.contentOffset.y + 600 > self.NewtableView.contentSize.height) {
//        NSLog(@"执行");
//        self.commentsCount += 20;
//        [self getData:self.str];
//        [self.NewtableView reloadData];
//    }
//    
//
//
//}








/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
